package producers;

import java.util.HashMap;
import java.util.Map;

import com.google.gson.JsonObject;

import net.sf.json.JSONObject;
import utils.WSUtil;

public class SIPAccount implements ISIPAccount {
    private final static String BASE_URL="https://api.ucpaas.com/2014-06-30";

    
    public String sid;
    
    public String token;
    
    public String appId;
    
    public SIPAccount(String sid,String token,String appId){
        this.sid = sid;
        this.token = token;
        this.appId = appId;
    }
    
    @Override
    public Map<String,String> create(String sid,String token,String appId,Map<String,String> options){
        Map<String,String> params = new HashMap<String,String>();
        params.put("appId", appId);
        params.put("clientType","0");
        params.put("charge", "0");
        if(null!=options){
            params.putAll(options);
        }
        JSONObject obj = new JSONObject();
        obj.put("client", JSONObject.fromObject(params));
        JsonObject rs = WSUtil.httpsPost(BASE_URL+Action.CREATE.getUrl(),sid, token, obj.toString());
        Map<String,String> result = parseResult(rs);
        return result;
    }
    
    @Override
    public Map<String,String> destroy(String sid,String token,String appId,String clientNumber){
        Map<String,String> params = new HashMap<String,String>();
        params.put("appId", appId);
        params.put("clientNumber",clientNumber);
        JSONObject obj = new JSONObject();
        obj.put("client", JSONObject.fromObject(params));
        Map<String,String> result = new HashMap<String,String>();
        JsonObject rs = WSUtil.httpsPost(BASE_URL+Action.DESTROY.getUrl(), sid, token, obj.toString());
        result.put("rcode",rs.get("respCode").getAsString());
        result.put("appId", this.appId);
        result.put("clientNumber", clientNumber);
        return result;
     
    }
    
    
    
    
    
    
    
    
    private Map<String,String> parseResult(JsonObject obj){
        Map<String,String> result = new HashMap<String,String>();
        result.put("rcode",obj.get("respCode").getAsString());
        if(obj.has("client")){
           JsonObject _json = obj.getAsJsonObject("client");
           result.put("balance", _json.get("balance").getAsString());
           result.put("clientNumber", _json.get("clientNumber").getAsString());
           result.put("clientPwd", _json.get("clientPwd").getAsString());
           result.put("createdAt", _json.get("createDate").getAsString());
        }else{
            result.put("balance", "");
            result.put("clientNumber", "");
            result.put("clientPwd", "");
            result.put("createdAt", "");
        }
        result.put("appId", this.appId);
        return result;
    }
    

    
    enum Action{
        CREATE("/Accounts/{sid}/Clients"),
        DESTROY("/Accounts/{sid}/dropClient");
       
        private String url;
        
        private Action(String url){
            this.url = url;
        }
        
        public String getUrl(){
            return this.url;
        }
        
    }
    
}
